package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.PreparedStatement;

import dto.StudentDto;

public class StudentDao {

	public String insertStudent(StudentDto student) {

		if (student == null)
			return "Insert failed! Input data is null.";
		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "INSERT INTO student "
				+ "(user_id, password) " + "VALUES (?,?)";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, student.getId());
			preparedStatement.setString(2, student.getPassword());
		//	preparedStatement.setInt(3, student.getUserId());

			preparedStatement.execute();
			response = "Inserted successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Insert failed! " + e.getMessage();
		}

		return response;
	}

	public String deleteStudent(String studentId) {

		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "DELETE FROM student WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, studentId);

			preparedStatement.execute();
			response = "Deleted successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Delete failed! " + e.getMessage();
		}

		return response;
	}

	public String updateStudent(StudentDto student) {

		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "UPDATE student SET password = ?, user_user_id = ?"
				+ " WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);

			preparedStatement.setString(1, student.getPassword());
			preparedStatement.setInt(2, student.getUserId());
			preparedStatement.setString(3, student.getId());

			preparedStatement.execute();
			response = "Updated successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Update failed! " + e.getMessage();
		}

		return response;
	}

	public StudentDto getStudentById(String studentId) {
		StudentDto student = null;

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM student WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, studentId);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				student = new StudentDto();
				student.setId(resultSet.getString("user_id"));
				student.setPassword(resultSet.getString("password"));
				student.setUserId(resultSet.getInt("user_user_id"));
			}

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return student;
	}

	public StudentDto getStudentByUserId(int userId) {
		StudentDto student = null;

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM student WHERE user_user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, userId);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				student = new StudentDto();
				student.setId(resultSet.getString("user_id"));
				student.setPassword(resultSet.getString("password"));
				student.setUserId(resultSet.getInt("user_user_id"));
			}

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return student;
	}

	public ArrayList<StudentDto> getAllStudent() {

		ArrayList<StudentDto> listStudent = new ArrayList<>();

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM student";

		Statement statement;
		try {
			statement = connection.createStatement();

			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				StudentDto student = new StudentDto();
				student.setId(resultSet.getString("user_id"));
				student.setPassword(resultSet.getString("password"));
				student.setUserId(resultSet.getInt("user_user_id"));
				listStudent.add(student);
			}

			statement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return listStudent;
	}

}
